﻿/*
 * Copyright (C) 2021. Huawei Device Co., Ltd. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the MIT License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * MIT License for more details.
 */

#ifndef UTILS_INTERVAL_FILTER
#define UTILS_INTERVAL_FILTER

#include <chrono>

class IntervalFilter {
public:
    explicit IntervalFilter(const long long interval)
    {
        m_interval = interval;
    }

    ~IntervalFilter() = default;

    bool Triger()
    {
        auto nowTriggerTime = std::chrono::system_clock::now();
        long long duration = std::chrono::duration_cast<std::chrono::milliseconds>(
            nowTriggerTime - lastTrigerTime).count();
        if (duration > m_interval) {
            lastTrigerTime = nowTriggerTime;
            return true;
        }
        return false;
    }

private:
    long long m_interval = 0;
    std::chrono::time_point<std::chrono::system_clock> lastTrigerTime;
};

#endif // !UTILS_INTERVAL_FILTER
